package com.nanjia.boot.manufacturer.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.core.security.util.SecurityUtils;
import com.nanjia.boot.manufacturer.converter.ManufacturerConverter;
import com.nanjia.boot.manufacturer.mapper.ManufacturerMapper;
import com.nanjia.boot.manufacturer.model.entity.Manufacturer;
import com.nanjia.boot.manufacturer.model.form.ManufacturerForm;
import com.nanjia.boot.manufacturer.model.query.ManufacturerQuery;
import com.nanjia.boot.manufacturer.model.vo.ManufacturerVO;
import com.nanjia.boot.manufacturer.service.ManufacturerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医疗器械生产商信息服务实现类
 *
 * @author 周洋
 * @since 2025-08-19 13:42
 */
@Service
@RequiredArgsConstructor
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements ManufacturerService {

    private final ManufacturerConverter manufacturerConverter;


    /**
    * 获取医疗器械生产商信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ManufacturerVO>} 医疗器械生产商信息分页列表
    */
    @Override
    public IPage<ManufacturerVO> getManufacturerPage(ManufacturerQuery queryParams) {
        Page<ManufacturerVO> pageVO = this.baseMapper.getManufacturerPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取医疗器械生产商信息表单数据
     *
     * @param id 医疗器械生产商信息ID
     * @return 医疗器械生产商信息表单数据
     */
    @Override
    public ManufacturerForm getManufacturerFormData(Long id) {
        Manufacturer entity = this.getById(id);
        return manufacturerConverter.toForm(entity);
    }
    
    /**
     * 新增医疗器械生产商信息
     *
     * @param formData 医疗器械生产商信息表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveManufacturer(ManufacturerForm formData, String ipAddr) {

        Manufacturer entity = manufacturerConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());
        return this.save(entity);
    }
    
    /**
     * 更新医疗器械生产商信息
     *
     * @param id   医疗器械生产商信息ID
     * @param formData 医疗器械生产商信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateManufacturer(Long id,ManufacturerForm formData) {
        Manufacturer entity = manufacturerConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除医疗器械生产商信息
     *
     * @param ids 医疗器械生产商信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteManufacturers(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的医疗器械生产商信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public Result<List<ManufacturerVO>> getManufacturerList(ManufacturerQuery queryParams) {
        LambdaQueryWrapper<Manufacturer> queryWrapper = new LambdaQueryWrapper<>();
        List<Manufacturer> list = this.list(queryWrapper);
        List<ManufacturerVO> voList = new ArrayList<>();
        voList = list.stream().map(manufacturerConverter::toVO).collect(Collectors.toList());
        return Result.success(voList);
    }
}
